package com.medical.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * 网关设置全局跨域
 */
//@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsFilter() {
        //设置跨域的配置信息
        CorsConfiguration config = new CorsConfiguration();
        //允许那些请求方式跨域
        config.addAllowedMethod("*");
        //允许所有请求来源跨域
        config.addAllowedOrigin("*");
        //允许那些头跨域
        config.addAllowedHeader("*");
        //是否允许携带cookie跨域,否则可能会丢失cookie信息
        config.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        //任意路径都需要跨域配置
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}
